package com.liyuan.yiqixing.controller;

import com.liyuan.yiqixing.model.Item;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.RecruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class RecruitController {
    @Autowired
    RecruitService recuritService;

    @PostMapping("/publishRecruitment")
    public void publishRecruitment(Item item, HttpServletResponse response) throws IOException {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        item.setUser(principal.getId());
        item.setHostelName(principal.getNickname());
        item.setImgUrl(principal.getAvatarUrl());
        recuritService.saveItem(item);
        response.sendRedirect("/");
    }
}
